package cn.tjie.basic.util;

import cn.tjie.basic.jwt.JwtUtils;
import cn.tjie.basic.jwt.LoginData;
import cn.tjie.basic.jwt.Payload;
import cn.tjie.basic.jwt.RsaUtils;
import cn.tjie.user.domain.Logininfo;

import javax.servlet.http.HttpServletRequest;
import java.security.PublicKey;

public class LoginContext {
    public static Logininfo getLogininfo(HttpServletRequest request){
        try {
            String jwtToken = request.getHeader("token");
            //获取公钥
            PublicKey publicKey = RsaUtils.getPublicKey(RsaUtils.class.getClassLoader().getResource("auth_rsa.pub").getFile());
            //解密
            Payload<LoginData> payload = JwtUtils.getInfoFromToken(jwtToken, publicKey, LoginData.class);
            //获取登录人信息
            Logininfo logininfo = payload.getLoginData().getLogininfo();
            //返回登录人信息
            return logininfo;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
